﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace www.coursera.algo.org._5.QuickSort
{
    class Program
    {
        static void Main(string[] args)
        {

            for (int i = 0; i <= 10; i++)
            {
                sortFile("array" + i + ".txt");
            }
            
            //List<double> list = new List<double>();
            //list.Add(3);
            //list.Add(6);
            //list.Add(4);
            //list.Add(2);
            
            //list.Add(5);

            //list=QuickSorter.median(list);
            //printList(list);
            sortFile("QuickSort.txt");
            Console.ReadLine();
        }

        private static void sortFile(string filename)
        {
            

            List<double> sortArray = new List<double>();
            sortArray.Clear();
            QuickSorter.count = 0;
            //{ 6, 5, 7, 4, 8, 3, 9, 2, 10, 1 };
            using (StreamReader sr = new StreamReader(filename))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    sortArray.Add(Convert.ToDouble(line));
                    //Console.WriteLine(line);
                }
            }

            
            //printList(sortArray);
            QuickSorter.SortFirst(sortArray, 0, sortArray.Count-1);
            //printList(sortArray);
            Console.Write("{0} low [{1}] ",filename, QuickSorter.count);


            sortArray.Clear();
            using (StreamReader sr = new StreamReader(filename))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    sortArray.Add(Convert.ToDouble(line));
                    //Console.WriteLine(line);
                }
            }

            QuickSorter.count = 0;
            
            QuickSorter.SortLast(sortArray, 0, sortArray.Count - 1);
            //printList(sortArray);
            Console.Write("{0} high [{1}] ", filename, QuickSorter.count);




            sortArray.Clear();

            using (StreamReader sr = new StreamReader(filename))
            {
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = sr.ReadLine()) != null)
                {
                    sortArray.Add(Convert.ToDouble(line));
                    //Console.WriteLine(line);
                }
            }

            QuickSorter.count = 0;

            QuickSorter.SortMedian(sortArray, 0, sortArray.Count - 1);
            //printList(sortArray);
            Console.WriteLine("{0} median [{1}] ", filename, QuickSorter.count);
            
            
        }

        private static void printList<T>(List<T> list)
        {
            foreach (var t in list)
            {
                Console.Write(t + " ");
            }
            Console.WriteLine();

        }
    }
}
